package net.zjitc.controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.CategoryService;
import net.zjitc.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/15 15:36
 * @description:
 */
//购物车相关
@Controller
public class AdminCart {
  @Autowired
  private CartService userCartService;
  @Autowired
  private CategoryService categoryService;
//查找购物车
  @RequestMapping("/findMyCart")
  public ModelAndView findMyCart(HttpSession session){
    ModelAndView mv =new ModelAndView();
    User user= (User) session.getAttribute("session_user");
    //如果未登录，则返回登录界面
    if (user==null){
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("/user/login.jsp"));
      return mv;
    }
    //如果登录
    List<CartItem> list=userCartService.findByUid(user.getUid());

    //如果购物车为空则直接转发
    if (list.size()==0){
      list=null;
    }
    if (list==null){
      //菜单栏显示一级菜单
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("/user/cart.jsp"));
      return mv;
    }
    mv.addObject("list",list);
    //计算总价
    double total=0.0;
    for (CartItem c:list){
      total=total+c.getQuantity()*c.getProduct().getShop_price();
    }
    mv.addObject("total",total);
    //菜单栏显示一级菜单
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    mv.setView(new InternalResourceView("/user/cart.jsp"));
    return mv;
  }


//添加购物车
  @RequestMapping("/addCart")
  public ModelAndView shopIndex(
      Product product,
      CartItem cartItem,
      HttpSession session
  ){
    ModelAndView mv= new ModelAndView();
    User user= (User)session.getAttribute("session_user");
    if (user==null){
      List<Category> clist=categoryService.findAll();
      mv.addObject("clist",clist);
      mv.setView(new InternalResourceView("/loginUI.do"));
      return mv;
    }
    System.out.println(product);
    cartItem.setProduct(product);
    cartItem.setUser(user);
    int count=userCartService.add(cartItem);
    mv.setView(new InternalResourceView("/findMyCart.do"));
    return mv;
  }
  //删除购物车
  @RequestMapping("/removeCart")
  public ModelAndView removeCart(
      @RequestParam(name = "cartid") String cartid
  ){
    ModelAndView mv= new ModelAndView();
    System.out.println(cartid);
    String abc = userCartService.removeCart(cartid);
    //菜单栏显示一级菜单
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    System.out.println(clist);
    mv.setView(new InternalResourceView("/user/cart.jsp"));
    return mv;
  }
  //清空购物车
  @RequestMapping("/clearCart")
  public ModelAndView clearCart(HttpSession Session){
        ModelAndView modelAndView=new ModelAndView();
    List<Category> clist=categoryService.findAll();
    User user= (User) Session.getAttribute("session_user");
    Integer count=userCartService.clearCart(user.getUid());
    modelAndView.addObject("clist",clist);
    modelAndView.setView(new InternalResourceView("/user/cart.jsp"));
        return modelAndView;
  }
}
